package pers.cz.tools;

import java.io.*;

/**
 * @program: PostGirl-panent
 * @description: 解析java文件
 * @author: Cheng Zhi
 * @create: 2022-05-19 20:16
 **/
public class JavaFileAnaylize {

    public static String anayLizeFile(File file) {

        try {
            // 读取文件拼接为字符串
            InputStream inputStream = new FileInputStream(file);
            StringBuilder resultStringBuilder = new StringBuilder();
            try (BufferedReader br
                         = new BufferedReader(new InputStreamReader(inputStream))) {
                String line;
                int i = 1;
                while ((line = br.readLine()) != null) {
                    if (i == 0) {
                        // 去掉第一行的包信息
                        continue;
                    }
                    // 判定这行是不是为空行,如果去掉空行，会导致开发人员根据行号定位代码有偏差
/*                    if (line.trim().length() != 0) {
                        resultStringBuilder.append(line).append("\n");
                    }*/
                    resultStringBuilder.append(line).append("\n");
                    i++;
                }
            }
            return resultStringBuilder.toString();
        } catch (Exception e) {
            System.out.println("解析java文件异常");
        }

        return null;
    }
}
